package cn.yzgy.yzgyoa.common.controller;


import cn.yzgy.yzgyoa.common.annotation.ApiDesc;
import cn.yzgy.yzgyoa.common.annotation.Syslog;
import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.common.dto.SysAlarmQueryDto;
import cn.yzgy.yzgyoa.common.entity.SysAlarm;
import cn.yzgy.yzgyoa.common.service.impl.SysAlarmServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.yzgy.yzgyoa.common.controller.BaseController;

/**
 * <p>
 * 系统异常告警，在系统遇到不正常数据时，会通过此表进行记录 前端控制器
 * </p>
 *
 * @author Yqsen
 * @since 2020-06-11
 */
@Syslog
@RestController
@RequestMapping("/common/alarm")
public class SysAlarmController extends BaseController {

    @Autowired
    private SysAlarmServiceImpl sysAlarmService;

    @GetMapping
    @ApiDesc("获取系统告警信息列表")
    @RequiresPermissions("api:common:SysAlarm:list")
    public Urmf getSysAlarmList(@RequestParam int page, @RequestParam int size, SysAlarmQueryDto queryDto) {
        IPage<SysAlarm> res = sysAlarmService.page(new Page<>(page, size), new QueryWrapper<SysAlarm>().lambda()
                .like(queryDto.getClassName() != null, SysAlarm::getClassName, queryDto.getClassName())
                .like(queryDto.getMethodName() != null, SysAlarm::getMethodName, queryDto.getMethodName())
                .like(queryDto.getErrDesc() != null, SysAlarm::getErrDesc, queryDto.getErrDesc())
                .between(queryDto.getStartTime() != null && queryDto.getEndTime() != null, SysAlarm::getGenerateTime,
                        queryDto.getStartTime(), queryDto.getEndTime())
                .orderByDesc(SysAlarm::getGenerateTime));
        res.getRecords().forEach(x -> x.setErrDesc(x.getErrDesc()
                .replace("\n", "<br/>")
                .replace("\r", "")));
        return Urmf.success(getCurrentMethodName(), res);
    }

}
